var totalAreaInPerches = 0.0;

$(function() {
    var he = 0,ac = 0,ro = 0,pe = 0,temp = 0;
    var cont = 2.47105381467;
    $('#hectareId').change(function() {
        changeHectare();
        calculateTotalPerches();
        $('#landExtent').val(totalAreaInPerches);
    });
    $('#acreId').change(function() {
        ac = $("#acreId").val();
        temp = parseFloat(ac % 1);
        if (temp >= 0) {
            $("#acreId").val(Math.floor(ac));
            ro = $("#roodsId").val();
            if (!$("#roodsId").val().length) {
                ro = 0;
            }
            temp = parseFloat(ro) + (temp * 4);
            var temp2 = parseFloat(temp % 1);

            $("#roodsId").val(Math.floor(temp));
            if (temp2 >= 0) {
                pe = $("#perchesId").val();
                if (!$("#perchesId").val().length) {
                    pe = 0;
                }
                temp = parseFloat(pe) + (temp2) * 40;
                $("#perchesId").val(temp.toFixed(4));
            }

        }
        setHect();
        calculateTotalPerches();
        $('#landExtent').val(totalAreaInPerches);
    });
    $('#roodsId').change(function() {
        ro = $("#roodsId").val();
        if (!$("#roodsId").val().length) {
            ro = 0;
        }
        temp = parseFloat(ro % 1);
        if (temp >= 0) {
            $("#roodsId").val(Math.floor(ro));
            pe = $("#perchesId").val();
            if (!$("#perchesId").val().length) {
                pe = 0;
            }
            temp = parseFloat(pe) + (temp) * 40;
            $("#perchesId").val(temp.toFixed(4));
        }
        checkRoodsLimit();
        setHect();
        calculateTotalPerches();
        $('#landExtent').val(totalAreaInPerches);
    });
    $('#perchesId').change(function() {
        checkPerchesLimit();
        setHect();
        calculateTotalPerches();
        $('#landExtent').val(totalAreaInPerches);
    });


});
function checkRoodsLimit() {
    var he = 0,ac = 0,ro = 0,pe = 0,temp = 0;
    ro = $("#roodsId").val();
    if (!$('#roodsId').val().length) {
        ro = 0;
    }
    ro = parseFloat(ro);
    temp = ro;
    ro = ro % 4;
    $("#roodsId").val(ro);
    ac = $("#acreId").val();
    if (!$('#acreId').val().length) {
        ac = 0;
    }
    ac = parseFloat(ac);
    ac = ac + ((temp - ro) / 4);
    $('#acreId').val(ac);

}
function checkPerchesLimit() {
    var he = 0,ac = 0,ro = 0,pe = 0,temp = 0;
    pe = $('#perchesId').val();
    if (!$('#perchesId').val().length) {
        pe = 0;
    }
    temp = pe;
    pe = parseFloat(pe);
    pe = pe % 40;
    $("#perchesId").val(pe);
    ro = $("#roodsId").val();
    if (!$('#roodsId').val().length) {
        ro = 0;
    }
    ro = parseFloat(ro);
    ro = ro + ((temp - pe) / 40);
    $("#roodsId").val(ro);
    checkRoodsLimit();
}
function setHect() {
    var he = 0,ac = 0,ro = 0,pe = 0,temp = 0;
    var cont = 2.47105381467;
    ac = $("#acreId").val();
    ro = $("#roodsId").val();
    pe = $("#perchesId").val();
    if (!$("#acreId").val().length) {
        ac = 0;
    }
    if (!$("#roodsId").val().length) {
        ro = 0;
    }
    if (!$("#perchesId").val().length) {
        pe = 0;
    }
    ac = parseFloat(ac);
    ro = parseFloat(ro);
    pe = parseFloat(pe);

    ro = parseFloat(ro + pe / 40);
    ac = parseFloat(ac + ro / 4);
    he = parseFloat(ac / cont).toFixed(4);
    $("#hectareId").val(he);
}
function changeHectare() {
    var he = 0,ac = 0,ro = 0,pe = 0,temp = 0;
    var cont = 2.47105381467;
    he = ($("#hectareId").val().length == 0) ? 0 : $("#hectareId").val();
    if (1 > he > 0) {
        he = parseFloat(he);
    }
    ac = Math.floor(he * cont);
    temp = (he * cont - ac) * 4;
    ro = Math.floor(temp);
    temp = (temp - ro) * 40;
    pe = temp.toFixed(4);
    $("#acreId").val(ac);
    $("#roodsId").val(ro);
    $("#perchesId").val(pe);
    $("#hectareId").val(he);
}

function calculateTotalPerches() {
    totalAreaInPerches = (parseFloat($('#acreId').val()) * 160) + (parseFloat($('#roodsId').val()) * 40) + parseFloat($('#perchesId').val());
}

$(function() {
    if (parseFloat($('#landExtent').val()) > 0) {
        fillExtent();
    }
});

function fillExtent() {
    totalAreaInPerches = $('#landExtent').val();
    if (!totalAreaInPerches.length) {
        totalAreaInPerches = 0.0;
    }
    totalAreaInPerches = parseFloat(totalAreaInPerches);
    var pe = totalAreaInPerches % 40;
    var ro = ((totalAreaInPerches - pe) / 40) % 4;
    var ac = ((((totalAreaInPerches - pe) / 40) - ro) / 4);
    var he = totalAreaInPerches * 0.002529285264;
    $("#acreId").val(ac);
    $("#roodsId").val(ro);
    $("#perchesId").val(pe.toFixed(4));
    $("#hectareId").val(he.toFixed(4));
}

function changeHectareKml() {
    
    var he = 0,ac = 0,ro = 0,pe = 0,temp = 0;
    var cont = 2.47105381467;
    he = $("#kmlArea").val() / 10000;

    if (1 > he > 0) {
        he = parseFloat(he);
    }
    ac = Math.floor(he * cont);
    temp = (he * cont - ac) * 4;
    ro = Math.floor(temp);
    temp = (temp - ro) * 40;
    pe = temp.toFixed(4);
    $("#kmlacreId").val(ac);
    $("#kmlroodsId").val(ro);
    $("#kmlperchesId").val(pe);
    $("#kmlhectareId").val(he.toFixed(4));
}